* ÓÅÑÕÅÎÔÉÁÌ ÔÅØÔ ÆÉÌÅÓ * ÂY ÁNDY ÓKELTON ÁDAPTED FROM THE WORKSHOP, "ÐROGRAMMING ÓEQUENTIAL ÆILES" PRESENTED AT THE 2ND ÁNNUAL ÍÁÒÃÁ ÃOMMODORE ÕSERS ÆAIR 7/27/85 (Ã)ÃOPYRIGHT 1985 BY Ê.Á. ÓKELTON. ÉNTRODUCTION: ÆILES AND THEIR ÕSES ---------------------------------- ÃOMMODORE COMPUTERS SUPPORT FOUR TYPES OF FILES: ÐÒOÇRAM, ÓÅÑUENTIAL, ÒÅÌATIVE (RANDOM ACCESS), AND ÕÓEÒ- DEFINED FILES. ×E'RE ALL PRETTY FAMILAR WITH ÐÒOÇRAM FILES; THESE MAKE OUR COMPUTERS DO WHAT THEY DO. ×E'RE ALSO QUITE ÕÎFAMILIAR WITH ÕÓEÒ FILES, AND WE'RE LIKELY TO REMAIN SO UNLESS WE GET INTO SERIOUS PROGRAMMING APPLICATIONS; BECAUSE THE STRUCTURE AND CONTENT OF ÕÓÒ FILES ARE ENTIRELY DETERMINED BY THE PROGRAMMER AND FEW GENERAL STATEMENTS CAN BE MADE. É'VE SEEN ÕÓÒ FILES IN GUISES RANGING FROM COMPILED VERSIONS OF ÂÁÓÉà PROGRAMS TO THE TEXT OF A FEW OBSCURE WORD PROCESSORS TO THE TITLES OF THE "PSEUDO" FILES CREATED BY ÄAVID'S ÄIRECTORY ÄESIGNER. ÔHIS LEAVES ÓÅÑUENTIAL AND ÒÅÌATIVE FILES. ÂY THEMSELVES, NEITHER CAN MAKE YOUR COMPUTER DO ANYTHING BUT, THEY ÓÔÏÒÅ DATA. ÂOTH TYPES CAN STORE STRUCTURED DATA, E.G., INFORMATION IN A MAILING LIST OR DATA BASE, ÂUT ONLY ÓÅÑUENTIAL FILES CAN STORE UNSTRUCTURED TEXT, E.G., WORD PROCESSOR FILES. Á DATA FILE IS "STRUCTURED" WHEN IT'S INTERNALLY ORGANIZED INTO ÒECORDS AND ÆIELDS; E.G., A FILE OF STUDENTS' GRADES FOR A SEMESTER IS ORGANIZED AROUND INDIVIDUAL STUDENTS (ÒECORDS), AND EACH STUDENT HAS THE SAME NUMBER OF GRADES (ÆIELDS) AS ANY OTHER STUDENT. Á TEXT FILE IS 'ORGANIZED' ONLY TO THE DEGREE THAT PARAGRAPHS, SENTENCES, AND WORDS IMPOSE A KIND OF MINIMAL ORGANIZATION; BUT THERE'S NO REQUIREMENT THAT EACH PARAGRAPH HAVE THE SAME NUMBER OF SENTENCES OR EACH SENTENCE THE SAME NUMBER OF WORDS. ÔHE PRESENT WORK IS CONCERNED WITH HOW ÓÅÑ TEXT FILES CAN BE USED IN CONJUNCTION WITH EXECUTING PROGRAMS. ÔEXT ÆILES IN ÐROGRAMS: ×HY? ---------------------------- Á CONVENIENT ATTRIBUTE OF ÓÅÑ FILES IS THAT THEY CAN BE STORED ON DISK OR TAPE. ÁNOTHER ADVANTAGE IS THAT ÓÅÑ FILES ARE "COMMON CURRENCY" IN THE WORLD OF COMPUTING; A ÓÅÑ FILE CREATED ON A ÃOMMODORE COMPUTER AND THEN UPLOADED TO AN ÁPPLE, AN ÉÂÍ, A ÔÒÓ-80, OR EVEN A MAINFRAME CAN BE READ BY THOSE COMPUTERS WITH MINIMAL HASSLES. ÔHE UNIVERSALITY OF ÓÅÑ FILES IS WHY É CAN SAY WRITE A SHORT HANDOUT FOR ONE OF MY CLASSES USING ÅASY ÓCRIPT, SEND THE FILE VIA MODEM TO THE ÖÁØ COMPUTER AT MY COLLEGE, AND HAVE THE ÖÁØ PRINT 40 COPIES ON A HIGHSPEED PRINTER. ÓUCH ADVANTAGES ARE GENERIC; THEY DON'T TELL YOU WHY ÓÅÑ FILES CAN BE ESPECIALLY HELPFUL IN PROGRAMMING. ÓO, LET'S GET MORE SPECIFIC. ×HEN YOU WRITE A PROGRAM INTENDED FOR USE BY OTHER PEOPLE, YOU INCLUDE ON-SCREEN INSTRUCTIONS FOR THE USER. ÂUT WHEN INSTRUCTIONS ARE LONG AND COMPLICATED, IT'S VERY TEDIOUS TO USE ÐÒÉÎÔ COMMANDS TO PRESENT THEM. ×HO HASN'T EXPERIENCED THE "JOY" OF INSERTING AND DELETING SPACES BETWEEN WORDS SO THAT THESE WILL SHOW UP PROPERLY ON THE 40-COLUMN SCREEN? ×HAT ABOUT THE "PLEASURE" OF EDITING A ÐÒÉÎÔ COMMAND LINE AND GETTING STUCK IN 'QUOTE MODE', SO THAT EVERYTHING YOU TYPE COMES OUT AS A GRAPHICS CHARACTER? ÆUN, RIGHT? É USED TO WRITE PROGRAMS IN JUST THAT WAY UNTIL É DISCOVERED A MUCH EASIER METHOD: É TYPE THE INSTRUCTIONS WITH MY WORD PROCESSOR AND SAVE THEM AS A ÓÅÑ FILE. ÔHEN, WHEN É WANT TO PRESENT ON-SCREEN HELP TO THE USER, É TELL THE PROGRAM TO DISPLAY THE FILE ON THE SCREEN. ÃONSIDER THIS NEWSLETTER: ÒIGHT NOW IF YOU READ THIS ARTICLE FROM DISK, YOUR READING -- A ÓÅÑ FILE -- FROM YOUR DISK AND DISPLAYING IT ON YOUR SCREEN OR PRINTER. ÔHE VERY CONCEPT OF A NEWSLETTER ON DISK DEPENDS ON THE ABILITY TO READ AND DISPLAY ÓÅÑ FILES FROM WITHIN AN ONGOING PROGRAM. ÔHE NEWSLETTER IDEA ALSO SHOWS ANOTHER WAY ÓÅÑ FILES CAN BE USED IN PROGRAMS: AS WAYS OF PASSING INFORMATION WHICH THE PROGRAM CAN USE FOR FURTHER OPERATIONS. ÎOT ONLY ARE ARTICLES ON THIS DISK A ÓÅÑ FILE, SO ARE MANY OF THE MENUS AND SUBMENUS. ÔHE MAIN ÎEWSLETTER MENU LETS YOU CHOOSE FROM AMONG ÍÅÎÕÓ ÁÒÔÉÃÌÅÓ ÐÒÏÇÒÁÍ ÉÎÓÔÒÕÃÔÉÏÎÓ ETC. ÉF YOU SELECT ÆÅÁÔÕÒÅ ÁÒÔÉÃÌÅÓ, YOU POP TO A SUBMENU WHICH SHOWS THE TITLES OF THE ARTICLES ON THIS DISK. ÔHE LIST OF TITLES ON YOUR SCREEN IS CONTAINED IN A ÓÅÑ FILE CALLED "ÁÒÔÉÃÌÅÓ". ×HEN YOU FIRST RUN THE ÍÅÎÕ PROGRAM, IT READS THIS FILE; THEN, THE ÍÅÎÕ DISPLAYS THE TITLES WHEN YOU SELECT THE ÁÒÔÉÃÌÅÓ SUBMENU. ×HEN YOU SELECT AN ARTICLE, THE PROGRAM DISPLAYS THAT ARTICLE. ÔHERE ARE MANY GOOD REASONS TO LEARN HOW TO INTEGRATE ÓÅÑ FILES INTO YOUR PROGRAMMING. ÌET'S LOOK AT HOW IT'S DONE. ÓTEPS IN ÒEADING ÆILES ------------------------ ÒEADING A ÓÅÑ FILE INCLUDES THE FOLLOWING STEPS: 1) ÙOU ÏÐÅÎ THE FILE. ÉF YOU WANT TO READ DATA FROM THE FILE, A TYPICAL COMMAND IS: 100 ÏÐÅÎ 3,8,3,"ÔÅØÔ,Ó,Ò" ÔHIS TELLS THE PROGRAM TO ACCESS AN ALREADY EXISTING FILE CALLED ÔÅØÔ. ÔHE ",Ó,Ò" PART OF THE COMMAND INDICATES THAT ÔÅØÔ IS A ÓÅÑ FILE TO BE ÒEAD INTO MEMORY. ÔHE NUMBERS "3,8,3" INDICATE THE FILE IS TO BE ASSIGNED FILE NUMBER 3 ON DEVICE NUMBER 8 (THE DISK DRIVE) AND BE READ OVER CHANNEL 3. ÔHE FILE NUMBER IS ARBITRARY -- ANY NUMBER UP TO 255 WILL DO (** ÎÏÔÅ:FILE NUMBERS OVER 127 WILL COME OUT DOUBLE SPACED....ÅÄ.) ÔHE DEVICE NUMBER IS MANDATORY -- 8 FOR A FILE ON DISK AND 1 FOR A FILE ON TAPE. ÔHE CHANNEL NUMBER ON A DISK DRIVE IS ANY NUMBER FROM 2 TO 14, BUT IT'S EASIEST TO USE THE SAME NUMBER AS THE FILE NUMBER; IF YOU USE A ÄATASETTE, THE CHANNEL NUMBER MUST BE 0. 2) ÙOU READ INFORMATION FROM THE FILE WITH THE COMMANDS, ÇÅÔ# OR ÉÎÐÕÔ#. ÆOR TEXT FILES, ÇÅÔ# IS PREFERABLE. 3) ×HEN THE END OF THE FILE IS ENCOUNTERED, YOU ÃÌÏÓÅ IT.Å.G.: 130 ÃÌÏÓÅ 3 ÆOLLOW THESE THREE STEPS ANY TIME YOU WANT TO OBTAIN INFORMATION FROM A ÓÅÑ FILE WHICH ALREADY EXISTS. ÆETCH AND ÄISPLAY ----------------- ÔÅØÔ IS AN UNSTRUCTURED FILE, SO HOW DO WE READ AND DISPLAY ITS CONTENTS? ×HAT ARE THE DETAILS OF STEP 2 ABOVE? ÔHESE PROGRAM LINES DO IT: 110 ÇÅÔ# 3,Á$:ÐÒÉÎÔ Á$; 120 ÉÆ ÓÔ<>64 ÔÈÅÎ ÇÏÔÏ 110 ÔHE LOGIC IS SIMPLE: WE DON'T KNOW HOW MANY CHARACTERS ARE IN ÔÅØÔ. ÓO WE TELL THE PROGRAM TO ÇÅÔ A CHARACTER (Á$), FROM ÔÅØÔ (WHICH WE'VE DESIGNATED AS FILE # 3) AND ÐÒÉÎÔ THE CHARACTER ON THE SCREEN (LINE 110). ÎOTE THAT THE SEMICOLON INSURES THAT THE CHARACTERS PRINT ÁÃÒÏÓÓ YOUR SCREEN. ÔHEN, THE PROGRAM CHECKS FOR THE VALUE OF A VARIABLE CALLED ÓÔATUS (LINE 120). ÓÔ IS A RESERVED, OR SYSTEM VARIABLE; ITS ROLE IS TO CHECK ON THE PROGRESS OF INPUT - OUTPUT OPERATIONS. ×HEN ÂÁÓÉà ENCOUNTERS THE END OF A DATA FILE, IT ASSIGNS A VALUE OF 64 TO ÓÔ. ÔHUS, ÓÔ CAN BE USED TO DETECT THE END OF A FILE. ÁS LONG AS ÓÔ IS ÎÏÔ 64, LINE 120 SENDS THE PROGRAM BACK TO LINE 110, TO FETCH AND DISPLAY ANOTHER CHARACTER FROM THE FILE. ×ITH THE FOUR PROGRAM LINES (100-130), YOU CAN READ AND DISPLAY ANY ÓÅÑ TEXT FILE! -- REMEMBER TO CHANGE THE FILE NAME IN THE ÏÐÅÎ COMMAND! ÕSE IN ÐROGRAMS --------------- ÅXTENDING THESE OPERATIONS INTO MORE COMPLEX PROGRAMS IS EASY. ÌINES 100- 130 CAN BE INCORPORATED AS A SUBROUTINE IN ANY PROGRAM. ×HEN YOU WANT TO DISPLAY A TEXT FILE FROM THE PROGRAM, SEND THE PROGRAM TO THE SUBROUTINE. ÎATURALLY, YOU'D HAVE A WAY TO INDICATE THE NAME OF THE ÓÅÑ FILE THAT'S TO BE DISPLAYED. Å.G., CONSIDER THESE LINES FROM A HYPOTHETICAL SWORD-AND- SORCERY ADVENTURE: 10 ÒÅÍ**ÄÉÓÐÌÁÙ ÓÔÁÒÔ ÉÎÓÔÒÕÃÔÉÏÎÓ** 15 ÐÒÉÎÔ"ÄÏ ÙÏÕ ×ÁÎÔ ÔÏ ÓÅÅ ÉÎÓÔÒÕÃÔÉÏÎÓ";:ÉÎÐÕÔ Ù$ 20 ÉÆ Ù$="Ù"ÔÈÅÎ ÆÌ$="ÉÎÓÔÒÕÃÔÉÏÎÓ" :ÇÏÓÕ 100 25 ÒÅÍ***ÄÉÓÐÌÁÙ ÃÈÁÒÁÃÔÅÒ ÄÅÓÃÒÉÐÔÉÏÎÓ*** 30 ÐÒÉÎÔ"×ÈÉÃÈ ÃÈÁÒÁÃÔÅÒ"*RM91:LM50 35 ÐÒÉÎÔ"1 -- ÁÇ×ÏÒ ÔÈÅ ÊÕÓÔ 40 ÐÒÉÎÔ"2 -- ÃÁÓÉÏ ÔÈÅ ÃÌÅÒÉà 45 ÐÒÉÎÔ"3 -- ÃÁÓÓÁÎÄÒÁ ÔÈÅ ÍÙÓÔÉà 50 ÉÎÐÕÔ"ÙÏÕÒ ÃÈÏÉÃÅ(1-3)";à 55 ÉÆ Ã=1 ÔÈÅÎ ÆÌ$="ÁÇ×ÏÒ" 60 ÉÆ Ã=2 ÔÈÅÎ ÆÌ$="ÃÁÓÉÏ" 65 ÉÆ Ã=3 ÔÈÅÎ ÆÌ$="ÃÁÓÓÁÎÄÒÁ" 70 ÇÏÓÕ 100: 99 ÒÅÍ***ÆÉÌÅ ÒÅÁÄÅÒ ÓÕÂÒÏÕÔÉÎÅ*** 100 ÏÐÅÎ 3,8,3,ÆÌ$+",Ó,Ò" 110 ÇÅÔ# 3,Á$:ÐÒÉÎÔ Á$; 120 ÉÆ ÓÔ<>64 ÔÈÅÎ ÇÏÔÏ 110 130 ÃÌÏÓÅ 3 140 ÒÅÔÕÒÎ ÔHE VALUE OF ÆÌ$, A STRING VARIABLE WHICH HOLDS THE NAME OF THE FILE, DETERMINES WHICH FILE IS READ BY THE SUBROUTINE. ÔHE VALUE OF ÆÌ$, IN TURN, DEPENDS UPON: (1)WHERE YOU ARE IN THE PROGRAM, AND (2)THE USER'S CHOICES. ÂY ALLOWING THE NAME OF ÆÌ$ TO VARY WITHIN THE PROGRAM, IT'S POSSIBLE TO READ MORE THAN ONE FILE DURING THE PROGRAM'S OPERATION USING THE SAME ROUTINE. ÂRASS ÔACKS ----------- ÔO USE TEXT FILES FROM A PROGRAM, É FIRST CREATE ALL THE TEXT FILES É WANT THE PROGRAM TO PRESENT. ÔHEN, É WRITE ONE OR MORE SUBROUTINES TO READ AND DISPLAY THE TEXT AT APPROPRIATE PLACES IN THE PROGRAM. ÌET'S LOOK AT EACH STEP. ÃREATING ÔEXT ÆILES: -------------------- ÌOAD ÅASY ÓCRIPT, OR ANY OTHER WORD PROCESSOR THAT SAVES TEXT AS ÓÅÑ FILES. ÔYPE THE TEXT, AND SAVE IT. ÔO INSURE THAT THE TEXT IS PROPERLY FORMATTED FOR A 40-COLUMN SCREEN DISPLAY, É INCLUDE A CARRIAGE RETURN AT THE END OF EACH LINE É TYPE INTO ÅASY ÓCRIPT. Å.G.: ÉÎÓÔÒÕÃÔÉÏÎÓ [ÒÅÔÕÒÎ] --------------[ÒÅÔÕÒÎ] ÔHE FIRST THING THE PROGRAM ASKS[ÒÅÔÕÒÎ] YOU TO ENTER IS YOUR NAME. ÔHEN,[ÒÅÔÕÒÎ] THE PROGRAM PROMPTS FOR YOUR AGE[ÒÅÔÕÒÎ] AND GRADE IN SCHOOL. ÁFTER THIS[ÒÅÔÕÒÎ] INFORMATION IS ENTERED..........[ÒÅÔÕÒÎ] ÔHIS IS ÍÕÃÈ EASIER THAN USING ÂÁÓÉà OR ÃÏÍÁÌ ÐÒÉÎÔ COMMANDS. ÁND IT GUARANTEES THERE'LL BE NO BROKEN WORDS AT THE END OF SCREEN LINES OR BLANK COLUMNS AT THE BEGINNING OF LINES. ×HAT IF YOUR WORD PROCESSOR DOES ÎÏÔ SAVE TEXT IN ÓÅÑ FILES? ÓOME ×ÐS (E.G., ÓPEEDÓCRIPT, ÓCRIPT 64, ÐAPER-CLIP> STORE TEXT AS ÐÒÇ FILES. ÔHE FILES AREN'T REALLY PROGRAMS; THE ×Ð STORES TEXT IN "SCREEN CODE FORMAT". ÙOU CAN STILL USE THE TECHNIQUE É'M RECOMMENDING IF YOU HAVE A UTILITY PROGRAM THAT TRANSLATES SCREEN-CODE ÐÒÇ FILES INTO ÓÅÑ FILES. ÔHERE IS A PROGRAM CALLED ÃONVERT 64. ÉT TRANSLATES ÓPEEDÓCRIPT FILES INTO ÓÅÑ FORM. ÉT MAY ALSO WORK WITH SCREEN-CODE FILES CREATED BY OTHER ×ÐS, BUT YOU'LL HAVE TO CHECK THIS YOURSELF. ÉF YOU USE ÐAPERCLIP (OR ÐAPARBACK [ÐOCKET] ×RITER), YOU NEEDN'T GO TO THIS TROUBLE BECAUSE ÐAPERCLIP LETS YOU CHOOSE BETWEEN SAVING TEXT AS A ÐÒÇ OR A ÓÅÑ FILE; CHOOSE THE ÓÅÑ METHOD. ÒEAD/ÄISPLAY ÓUBROUTINES: ------------------------- ÏNE OR MORE SUBROUTINES LIKE THE ONE SHOWN EARLIER CAN BE BUILT INTO YOUR PROGRAM. ÙOU CAN ADD FEATURES TO PERMIT MORE CONTROLLED ON-SCREEN DISPLAYS. ÆOR EXAMPLE, YOU CAN INCLUDE A LINE-COUNTER VARIABLE SO THAT THE SCREEN DISPLAY OF THE TEXT FILE TEMPORARILY HALTS AFTER A CERTAIN NUMBER OF LINES ARE SHOWN [24 FOR ÃOMMODORE]; THE DISPLAY RESUMES ONLY AFTER THE USER DOES SOMETHING. Å.G.: 99 ÒÅÍ ***ÆÁÎÃÙ ÆÉÌÅ ÄÉÓÐÌÁÙ*** 100 ÏÐÅÎ 3,8,3,ÆÌ$+",Ó,Ò":ÌÃ=1 110 ÇÅÔ#3, Á$;ÐÒÉÎÔ Á$; 112 ÉÆ Á$=ÃÈÒ$(13) ÔÈÅÎ ÌÃ=ÌÃ+1 114 ÉÆ ÌÃ/20=ÉÎÔ(ÌÃ/20)ÔÈÅÎÇÏÓÕÂ200 120 ÉÆ ÓÔ<>4 ÔÈÅÎ ÇÏÔÏ 110 130 ÃÌÏÓÅ 3 140 ÒÅÔÕÒÎ 199 ÒÅÍ ***ÐÒÅÓÓ Á ËÅÙ*** 200 ÐÒÉÎÔ"ÐÒÅÓÓ ÁÎÙ ËÅÙ ÔÏ ÃÏÎÔÉÎÕÅ" 210 ÇÅÔ Ò$:ÉÆ Ò$=""ÔÈÅÎ ÇÏÔÏ 210 220 ÐÒÉÎÔ"<ÓÈÉÆÔ/ÃÌÒ>" 230 ÒÅÔÕÒÎ ÔHE LINE COUNTER, ÌÃ, IS INITIALIZED TO 1 IN LINE 100. ×HENEVER THE FILE READ SUBROUTINE ENCOUNTERS A CARRIAGE RETURN (WE'VE ALREADY MADE SURE THERE'S A [ÒÅÔÕÒÎ] AT THE END OF EACH LINE), Ìà INCREASES BY 1 (LINE 112). ×HEN Ìà REACHES A VALUE THAT CAN BE EVENLY DIVIDED BY 20, THE PROGRAM GOES TO THE SUBROUTINE BEGINNING IN LINE 200. ÔHIS ROUTINE WAITS FOR YOU TO PRESS A KEY. ×HEN YOU DO, THE SCREEN IS CLEARED AND YOU RETURN TO THE EARLIER SUBROUTINE, WHICH READS AND DISPLAYS 20 MORE LINES OF TEXT FROM THE ÓÅÑ FILE. ÔHIS IS USEFUL WHEN YOUR TEXT FILE IS TOO LONG TO FIT ONTO A SINGLE 25 LINE SCREEN. ÙOU CAN PROBABLY THINK OF OTHER VARIATIONS. ÆOR INSTANCE, IN ITS CURRENT FORM, THE FILE READER SUB-ROUTINE PUTS A NEW CHARACTER ON THE SCREEN AS SOON AS THE CHARACTER IS READ FROM THE FILE. ÔHIS CAUSES THE CURSOR TO SCROLL ACROSS YOUR SCREEN TO THE RIGHT, LEAVING LETTERS BEHIND IT. ÔHE SUBROUTINE CAN BE RE-WRITTEN SO AS NOT TO DISPLAY ANYTHING UNTIL A COMPLETE LINE OF TEXT HAS BEEN READ FROM THE FILE. ÁNOTHER VARIATION CAN MAKE THE FILE READER ACT MUCH LIKE THE NEWSLETTER: ÎEW LINES OF TEXT ARE DISPLAYED ONLY WHEN YOU PRESS A CERTAIN KEY. ÙOU CAN EVEN CAUSE THE TEXT TO BE HELD IN MEMORY AND ONLY DISPLAYED AT A LATER POINT IN THE PROGRAM, LIKE THE NEWSLETTER SUBMENUS. É HOPE YOU'VE BEGUN TO GLIMPSE SOME OF THE POSSIBILITIES OF USING SEQUENTIAL TEXT FILES WITHIN PROGRAMS. ÆEEL FREE TO MODIFY THE PROGRAM TO READ OTHER FILES, OR INCORPORATE IT INTO YOUR OWN PROGRAMMING PROJECTS. ***************************************************************************** ÅÄÉÔÏÒÓ ÎÏÔÅ ------------ ÔHE ABOVE FILE WAS WRITTEN TO GO IN A NEWSLETTER THAT USED DIFFERENT MENU AND SUBMENU ROUTINES THAN THIS MAGAZINE DISK USES SO DON'T EXPECT IT TO MATCH EXACTLY OUR ROUTINES USED HERE. É HAVE NOT CHANGED IT BECAUSE É FELT OUR READERS WOULD LIKE TO SEE THE ARTICLE AS WRITTEN.